package com.itheima.pinda.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.pinda.DTO.transportline.TransportLineDto;
import com.itheima.pinda.common.utils.Constant;
import com.itheima.pinda.common.utils.PageResponse;
import com.itheima.pinda.entity.transportline.PdTransportLine;
import com.itheima.pinda.mapper.PdTransportLineMapper;
import com.itheima.pinda.mapstruct.TransportDTO2Entity;
import com.itheima.pinda.service.PdTransportLineService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@Service
public class PdTransportLineServiceImpl extends ServiceImpl<PdTransportLineMapper, PdTransportLine> implements PdTransportLineService {

    @Resource
    private TransportDTO2Entity dto2Entity;

    /**
     * 添加线路
     *
     * @param dto 线路信息
     * @return 线路信息
     */
    @Override
    public TransportLineDto saveTransportLine(TransportLineDto dto) {
        PdTransportLine pdTransportLine = dto2Entity.conver(dto);
        super.save(pdTransportLine);
        dto = dto2Entity.conver(pdTransportLine);
        return dto;
    }

    /**
     * 获取线路分页信息
     *
     * @param page                页码
     * @param pageSize            页尺寸
     * @param lineNumber          线路编号
     * @param name                线路名称
     * @param transportLineTypeId 线路类型id
     * @return 线路分页信息
     */
    @Override
    public PageResponse<TransportLineDto> findByPage(Integer page, Integer pageSize,
                                                     String lineNumber, String name, String transportLineTypeId) {
        Page<PdTransportLine> pdTransportLinePage = super.page(new Page<>(page, pageSize), Wrappers.<PdTransportLine>lambdaQuery()
                .like(StringUtils.isNotBlank(name), PdTransportLine::getName, name)
                .like(StringUtils.isNotBlank(lineNumber), PdTransportLine::getLineNumber, lineNumber)
                .eq(StringUtils.isNotBlank(transportLineTypeId), PdTransportLine::getTransportLineTypeId, transportLineTypeId)
                .eq(PdTransportLine::getStatus, Constant.DATA_DEFAULT_STATUS)
                .orderByAsc(PdTransportLine::getId));

        return PageResponse.<TransportLineDto>builder()
                .page(page)
                .pagesize(pageSize)
                .pages(pdTransportLinePage.getPages())
                .counts(pdTransportLinePage.getTotal())
                .items(pdTransportLinePage.getRecords()
                        .stream()
                        .map(item -> dto2Entity.conver(item))
                        .collect(Collectors.toList()))
                .build();
    }

    /**
     * 获取线路列表
     *
     * @param ids       线路id列表
     * @param agencyId  机构Id
     * @param agencyIds 机构Id列表
     * @return 线路列表
     */
    @Override
    public List<TransportLineDto> findAll(List<String> ids, String agencyId, List<String> agencyIds) {
        List<PdTransportLine> pdTransportLineList = super.list(Wrappers.<PdTransportLine>lambdaQuery()
                .eq(StringUtils.isNotBlank(agencyId), PdTransportLine::getAgencyId, agencyId)
                .in(ids != null && ids.size() > 0, PdTransportLine::getId, ids)
                .in(agencyIds != null && agencyIds.size() > 0, PdTransportLine::getAgencyId, agencyIds)
                .eq(PdTransportLine::getStatus, Constant.DATA_DEFAULT_STATUS)
                .orderByAsc(PdTransportLine::getId));

        return pdTransportLineList.stream()
                .map(item -> dto2Entity.conver(item))
                .collect(Collectors.toList());
    }

    /**
     * 获取线路列表
     *
     * @return 线路列表
     */
    @Override
    public List<TransportLineDto> getList(TransportLineDto transportLineDto) {
        List<PdTransportLine> pdTransportLineList = super.list(Wrappers.<PdTransportLine>lambdaQuery()
                .eq(StringUtils.isNotBlank(transportLineDto.getStartAgencyId()), PdTransportLine::getStartAgencyId, transportLineDto.getStartAgencyId())
                .eq(StringUtils.isNotBlank(transportLineDto.getEndAgencyId()), PdTransportLine::getEndAgencyId, transportLineDto.getEndAgencyId())
                .eq(StringUtils.isNotBlank(transportLineDto.getAgencyId()), PdTransportLine::getAgencyId, transportLineDto.getAgencyId())
                .eq(Objects.nonNull(transportLineDto.getStatus()), PdTransportLine::getStatus, transportLineDto.getStatus()));

        return pdTransportLineList.stream()
                .map(item -> dto2Entity.conver(item))
                .collect(Collectors.toList());
    }
}
